<template>
  <div class="advert-space-list">
    <el-card>
      <div slot="header">
        <el-button
          type="primary"
          @click="$router.push({
            name: 'advert-space-create'
          })"
        >添加广告位</el-button>
      </div>
      <el-table
        :data="advertSpaces"
        v-loading="loading"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column
          prop="spaceKey"
          label="spaceKey"
          width="120">
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告位名称"
          width="120">
        </el-table-column>
        <el-table-column
          label="创建时间">
          <template slot-scope="scope">
            <div>{{dateFormatter(scope.row.createTime)}}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="更新时间">
          <template slot-scope="scope">
            <div>{{dateFormatter(scope.row.updateTime)}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="price"
          label="操作"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="$router.push({
                name: 'advert-space-edit',
                params: {
                  spaceId: scope.row.id
                }
              })"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllSpaces } from '@/services/advert-space'
import { Form } from 'element-ui'
import moment from 'moment'

export default Vue.extend({
  name: 'AdSpacesList',
  data () {
    return {
      advertSpaces: [],
      loading: true
    }
  },

  created () {
    this.loadAdvertSpaces()
  },

  methods: {
    async loadAdvertSpaces () {
      this.loading = true
      const { data } = await getAllSpaces()
      this.advertSpaces = data.content
      this.loading = false
    },

    dateFormatter (date: Date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }
  }
})
</script>

<style lang="scss" scoped>
.el-card {
  margin-bottom: 20px;
}
</style>
